package Practice.P02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class PokerGameOrder02 {
    // 准备牌
    // 给牌添加价值
    static ArrayList<String> list = new ArrayList<>();
    static HashMap<String,Integer> hm = new HashMap<>();
    static {
        // 牌的构成
        String[] color = {"♥","♠","♦","♣"};
        String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        for (String c : color) {
            for (String n : number) {
                list.add(c+n);
            }
        }
        list.add(" 小王");
        list.add(" 大王");
        hm.put("J",11);
        hm.put("Q",12);
        hm.put("K",13);
        hm.put("A",14);
        hm.put("2",15);
        hm.put("小王",50);
        hm.put("大王",100);

    }
    // 发牌
    public PokerGameOrder02(){
        // 发牌
        Collections.shuffle(list);
        ArrayList<String> lord = new ArrayList<>();
        ArrayList<String> play1 = new ArrayList<>();
        ArrayList<String> play2 = new ArrayList<>();
        ArrayList<String> play3 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String poker = list.get(i);
            if(i<=2){
                lord.add(poker);
                continue;
            }
            if(i%3==0){
                play1.add(poker);
            } else if (i%3==1) {
                play2.add(poker);
            }else {
                play3.add(poker);
            }
        }
        // 价值排序操作
        order(lord);
        order(play1);
        order(play2);
        order(play3);
        // 查看牌
        lookPoker("底牌",lord);
        lookPoker("玩家1",play1);
        lookPoker("玩家2",play2);
        lookPoker("玩家3",play3);

    }
    public void order(ArrayList<String> list){
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String color1 = o1.substring(0, 1);
                int value1 = getValue(o1);
                String color2 = o2.substring(0, 1);
                int value2 = getValue(o2);
                int i = value1 - value2;

                return i==0 ? color1.compareTo(o2):i;
            }
        });
    }

    public int getValue(String poker){
        String number = poker.substring(1);
        if(hm.containsKey(number)){
            return hm.get(number);
        }else {
            return Integer.parseInt(number);
        }
    }
    // 看牌
    public void lookPoker(String name,ArrayList<String> list){
        System.out.print(name+":");
        for (String s : list) {
            System.out.print(s+" ");
        }
        System.out.println();
    }


}
